//
//  AELTrackControlModel.swift
//  ActualExaminationLine
//
//  Created by iOS-Czz on 2023/10/30.
//

import UIKit

class AELTrackControlViewModel{
    var dataSource = [AELTrackControlModel]()
    var resetvationCount = 0
    var bookingCount = 0
}
extension AELTrackControlViewModel{
    func loadData(onSuccess: ((_ models: [AELTrackControlModel]) ->Void)?){
        let dataHelper = AELTrackControlModel.getUsingLKDBHelper()
        let sql = "select * from @t"
        let modelArr = dataHelper.search(withSQL: sql, to: AELTrackControlModel.self) as! [AELTrackControlModel]
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "yyyy.MM.dd"
        let sortedModels = modelArr.sorted { (model1, model2) -> Bool in
            guard let date1 = dateFormatter.date(from: model1.date),
                  let date2 = dateFormatter.date(from: model2.date) else {
                return false // 如果转换失败则直接返回 false
            }
            return date1 < date2
        }
        self.bookingCount = sortedModels.filter { $0.type == 1 }.count
        self.resetvationCount = sortedModels.filter { $0.type == 0 }.count
        self.dataSource = sortedModels
        onSuccess?(self.dataSource)
    }
    
}

@objcMembers
class AELTrackControlModel: AELBaseModel,Codable {
    var name = ""
    var type = 0
    var time = ""
    var date = ""
    var duration = ""
    var isDefault = true
    override class func getTableName() -> String {
        return  String(describing: AELTrackControlModel.self)
    }
}
